﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [AppService]
    public class AdminUserGroupService
    {
        private readonly AdminUserGroupRepository adminGroupDal;
        private readonly AdminUserRepository _adminUser;
        private readonly ICacheManager _cacheManager;
        private readonly string cacheKey;
        public AdminUserGroupService(AdminUserGroupRepository adminUserGroupRepository, AdminUserRepository adminUser, ICacheManager CacheManager)
        {
            adminGroupDal = adminUserGroupRepository;
            _adminUser = adminUser;
            _cacheManager = CacheManager;
            cacheKey = CacheConst.CACHE_KEY_USERGROUP;
        }

        public async Task<List<AdminUserGroup>> GetListAsync()
        {
            return await adminGroupDal.GetAllListAsync("Id DESC");
        }
        public async Task<Page<AdminUserGroup>> GetPageListAsync(int page, int limit)
        {
            return await adminGroupDal.GetPageListAsync(page, limit);
        }

        /// <summary>
        /// 获取一条角色信息并缓存
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUserGroup> GetAsync(int? id)
        {
            AdminUserGroup adminUserGroup = _cacheManager.Get<AdminUserGroup>(cacheKey + id);
            if (adminUserGroup == null)
            {
                adminUserGroup = await adminGroupDal.GetAsync(id);
                _cacheManager.Set(cacheKey + id, adminUserGroup, TimeSpan.FromDays(31));
            }

            return adminUserGroup;
        }

        /// <summary>
        /// 获取一条角色信息不缓存
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUserGroup> GetNoCacheAsync(int? id)
        {
            return await adminGroupDal.GetAsync(id);
        }

        public async Task<bool> GetByGroupNameAsync(string groupName)
        {
            return await adminGroupDal.GetByGroupNameAsync(groupName);
        }


        public virtual async Task<object> InsertAsync(AdminUserGroup adminUserGroup)
        {
            return await adminGroupDal.InsertAsync(adminUserGroup);
        }


        public async Task<int> UpdateAsync(AdminUserGroup adminUserGroup, int id)
        {
            AdminUserGroup userGroup = await GetAsync(id);
            userGroup.Groupname = adminUserGroup.Groupname;
            userGroup.Systemrole = adminUserGroup.Systemrole;
            _cacheManager.Remove(cacheKey + id);
            return await adminGroupDal.UpdateAsync(adminUserGroup);
        }


        public async Task<bool> DeleteByPriKeyAsync(int id)
        {
            if (await _adminUser.GetByGidAsync(id))
                return false;
            else
            {
                _cacheManager.Remove(cacheKey + id);
                return await adminGroupDal.DeleteByPriKeyAsync(id) > 0;
            }
                
        }


    }
}
